#include "for_test_message.h"

uint32_t adc_loop = 0;
uint32_t adc_loop_last = 0;
uint32_t bemf_loop = 0;
uint32_t bemf_in_PWM_loop = 0;
uint32_t bemf_high_loop = 0;
char is_pwm_on = 0;
char adc_message_tips_timer = 0;
extern uint32_t disable_pwm_counters;

RCC_ClocksTypeDef systick_get;
uint32_t system_clock;
extern Whole_Motor_Struct Motor1;
extern __IO uint16_t u16ADC1[10];
extern __IO uint16_t u16SystemTime1ms;
extern __IO uint16_t u16SystemTime200us;
extern __IO uint16_t u16SystemTime100us;
uint32_t ADCR_working = 0;
uint32_t ADCR_working_construct = 0;
char is_in_adc_loop = 0;
int in_loop_state = 0;
uint32_t adc_loop_channel_6_data_changeed = 0;
uint32_t adc_loop_last_channel_6_data_now = 0;
uint32_t adc_loop_last_channel_6_data_last = 0;
uint32_t uchecked_loop_state = 0;
void going_check_state(int state)
{
    in_loop_state = state;
}
void check_adc_loop_channel_6_data_changeed(void)
{
    if (adc_message_tips_timer > 10)
    {

        if (adc_loop_last_channel_6_data_last != ADC1->ADDR6)
        {
            if (adc_loop_channel_6_data_changeed++ > 100000000)
            {
                adc_loop_channel_6_data_changeed = 0;
            }
            adc_loop_last_channel_6_data_last = ADC1->ADDR6;
        }
    }
}

void check_time_menage_canrun(void)
{
    if (adc_message_tips_timer > 10)
    {
        // u16SystemTime100us = 0;
        u16SystemTime200us = 0;
        u16SystemTime1ms = 0;
        // Motor1.command.u8MotorRunMode = 5;
    }
}
void going_adc_loop(void)
{
    is_in_adc_loop = 1;
}
void outing_adc_loop(void)
{
    is_in_adc_loop = 2;
}
void tim_status_message(void)
{
    printf("tim1_period:%d tim_ccr1:%d tim_ccr2:%d tim_ccr3:%d tim_ccr4:%d ,tim_cnt:%d \r\n", PWM_PERIOD, (uint32_t)TIM1->CCR1, (uint32_t)TIM1->CCR2, (uint32_t)TIM1->CCR3, (uint32_t)TIM1->CCR4, (uint32_t)TIM1->CNT);
}
void adc_status_message(void)
{
    printf("ADDR0:%x,ADDATA:%x ,ADCFG: %x , ADCR: %x, ADCHS: %x , ADCMPR: %x ,ADSTA: %x , RESERVED0:%x,ADC_ADSTA_EXT:%x \r\n", ADC1->ADDR0, ADC1->ADDATA, ADC1->ADCFG, ADC1->ADCR, ADC1->ADCHS, ADC1->ADCMPR, ADC1->ADSTA, ADC1->RESERVED0, ADC1->ADC_ADSTA_EXT);
}
void adc_check_test(void)
{
    if (ADC1->ADCR == 0x40305)
    {
        if (ADCR_working++ > 10000000)
        {
            ADCR_working = 0;
        }
    }
}
void adc_loop_check_1ms(void)
{
    // adc_check_test();
    if (adc_loop_last == adc_loop)
    {
        // TIM1->CCR4 = 0;
        // TIM1->CCR5 = 0;
        if (adc_message_tips_timer < 10)
        {
            // printf("adc_loop_stoped!!\r\n");
            // printf("adc_loop:%d\r\n", adc_loop);
            // ADC1->ADCR &= ADCR_SWSTART_Reset;
            // printf("ADC1->ADSTA:%x,ADC1->ADCR:%x \r\n", ADC1->ADSTA, ADC1->ADCR);
            adc_message_tips_timer++;
        }
        {

            //  while (adc_message_tips_timer <= 10)
            {
                // printf("adc_loop:%d\r\n", adc_loop);
                // printf("middle_data: %d ,u16TableAddress:%d ,ROTOR_IPD1_TARGET_CURRENT * CURRENT_GAIN:%d \r\n", ((int32_t)IPD_Sin(Motor1.IPD.u16TableAddress) * (-(ROTOR_IPD1_TARGET_CURRENT * CURRENT_GAIN) / 10)), Motor1.IPD.u16TableAddress, ROTOR_IPD1_TARGET_CURRENT * CURRENT_GAIN);
                // adc_status_message();
                // printf(" run: %d\r\n", Motor1.command.u8MotorRunMode); //获取运行状态
                // u16SystemTime1ms = 0;
                // ADC1->ADSTA = (uint16_t)0x1000;
            }
        }
    }
    if (adc_message_tips_timer >= 10)
    {
        // printf("adc_loop:%d\r\n", adc_loop);
        // printf("middle_data: %d ,u16TableAddress:%d ,ROTOR_IPD1_TARGET_CURRENT * CURRENT_GAIN:%d \r\n", ((int32_t)IPD_Sin(Motor1.IPD.u16TableAddress) * (-(ROTOR_IPD1_TARGET_CURRENT * CURRENT_GAIN) / 10)), Motor1.IPD.u16TableAddress, ROTOR_IPD1_TARGET_CURRENT * CURRENT_GAIN);
        // adc_status_message();
        // printf(" run: %d\r\n", Motor1.command.u8MotorRunMode); //获取运行状态
        //  ADC1->ADCR &= ADCR_SWSTART_Reset;
        if (adc_message_tips_timer == 10)
        {
            TIM1->CCR4 = 0;
            TIM1->CCR5 = 0;
            ADC1->ADCR &= ADCR_SWSTART_Reset;
            ADC1->ADCR &= ADCR_EXTTRIG_Reset;
            ADC1->ADCHS |= 0x00000180;
            ADC1->ADCFG &= ADCFG_ADEN_Reset;
            // ADC1->ADCR &= ADCFG_ADEN_Reset;
            // ADC_Cmd(ADC1, DISABLE);
            //  while (1)
            //       ; // ADC1->ADSTA = (uint16_t)0x1000;
            adc_message_tips_timer = 0;
            adc_message_tips_timer = 11;
            TIM_Cmd(TIM1, DISABLE);
            while (1)
            {
                /* code */
            }
            uchecked_loop_state++;
        }
        uchecked_loop_state++;
    }

    adc_loop_last = adc_loop;
    if (Motor1.command.u8MotorRunMode == 4)
    {
        adc_message_tips_timer = 10;
    }
}

void bemf_print_adc(void)
{
    printf(" Va:%d,Vb:%d,Va_now:%d,Bemf_A_Zero:%d,Bemf_B_zero:%d \r\n***bemf_Rotor_IPD_Mode_Process*****", u16ADC1[BEMFA_ADC_CHANNEL], u16ADC1[BEMFB_ADC_CHANNEL], (uint16_t)BEMFA_ADC_DATA_REGISTER, Motor1.IPD.u16BEMFPhaseAZero12bit, Motor1.IPD.u16BEMFPhaseBZero12bit);
}
void motor_stopped_message(void)
{
    if (Motor1.command.u8MotorRunMode != 0)
    {
        printf("**stop!!!!!!*****\r\n");
        printf("error: %d", Motor1.info.u8ErrorCode);      //测试uart输出
        printf(" run: %d", Motor1.command.u8MotorRunMode); //获取运行状态
        printf("code: %d", Motor1.command.u8MotorStart);
        printf("motor_start:%d  errorcounter:%d\r\n", Motor1.command.u8MotorStart, Motor1.info.u8ErrorCounter);
    }
}
void adc_loop_check_message(void)
{
    printf("adc_loop: %d\r\n", adc_loop);
}
void bemf_adc_counters(void)
{
    if (bemf_loop++ > 10000000)
    {
        bemf_loop = 0;
    }
    if (is_pwm_on)
    {
        if (bemf_in_PWM_loop++ > 10000000)
        {
            bemf_in_PWM_loop = 0;
        }
    }
    if (u16ADC1[BEMFA_ADC_CHANNEL] > 1200 || u16ADC1[BEMFB_ADC_CHANNEL] > 1200)
    {
        if (bemf_high_loop++ > 100000000)
        {
            bemf_high_loop = 0;
        }
    }
}
void adc_loop_counters(void)
{

    if (adc_loop++ > 100000000)
    {
        adc_loop = 0;
    };

    //计算进入了几次
    // printf("ready????");
}

void uart_output_message(void)
{
    system_clock = systick_get.SYSCLK_Frequency;
    LED2_TOGGLE(); //测试板子是否正常运作
    printf("*******\r\n");
    printf("error: %d,", Motor1.info.u8ErrorCode);      //测试uart输出
    printf(" run: %d,", Motor1.command.u8MotorRunMode); //获取运行状态
    printf("code: %d,", Motor1.command.u8MotorStart);
    printf("motor_start:%d  errorcounter:%d,", Motor1.command.u8MotorStart, Motor1.info.u8ErrorCounter);
    printf(" voltage_averaged: %d,", Motor1.info.u16VSPAverage); //获取运行状态
    printf(" voltage: %d\r\n", Motor1.info.u16VSPInput);         //获取电压
    printf("addata:%d,  ", (uint32_t)ADC2->ADDATA);
    printf("IAerror:%d,IAzero:%d,ADC_Anow:%d, ", Motor1.FOC.s16Ia, CurrentSensing.s16IaZero, u16ADC1[Ia_ADC_CHANNEL]);
    printf("IBerror:%d,IBzero:%d,ADC_Bnow:%d\r\n", Motor1.FOC.s16Ib, CurrentSensing.s16IbZero, u16ADC1[Ib_ADC_CHANNEL]);
    //		printf(" adc_loop: %d,theta_loop:%d\r\n", adc_loop, i_theta_loop);
    printf(" speed_bemf: %d,Va:%d,Vb:%d,adcVanow:%d,adcVbnow:%d,Bemf_A_Zero:%d,Bemf_B_zero:%d \r\n", Motor1.BEMF.u16BEMFSpeed, u16ADC1[BEMFA_ADC_CHANNEL], u16ADC1[BEMFB_ADC_CHANNEL], (uint16_t)BEMFA_ADC_DATA_REGISTER, (uint16_t)BEMFB_ADC_DATA_REGISTER, Motor1.IPD.u16BEMFPhaseAZero12bit, Motor1.IPD.u16BEMFPhaseBZero12bit);
    printf(" speed_target_originol: %d,", Motor1.spec.u16OpenLoopTargetSpeed);
    printf(" speed: %d,speed_target:%d,speed_cmd_now:%d,speed_ipd:%d,angle:%d,s16Vd:%d,Thrta:%d\r\n", Motor1.smo.s16Speed, Motor1.command.u16SpeedTarget, Motor1.command.u16SpeedCommandNow, Motor1.IPD.s16Speed, Motor1.command.u16Angle, Motor1.FOC.s16Vd, Motor1.smo.s16Theta);
    //		printf("change_loop_smo:%d,change_loop_cmd:%d,q15:%d,Valpha_foc:%d,Vbeta_foc:%d\r\n", i_smo_speed_change_times, i_speed_cmd_changed, Motor1.smo.s16SpeedEstGainQ15, Motor1.FOC.s16Valpha, Motor1.FOC.s16Vbeta);
    printf("u16bus:%d u16vollim:%d\r\n", Motor1.info.u16VBusVoltageAverage, DC_BUS_OVER_VOLTAGE_LIMITATION);
    tim_status_message();
    adc_status_message();
    printf("adc_loop: %d,bemf_in_PWM_loop:%d,bemf_loop:%d,bemf_high_loop:%d,bz_flag: %d\r\n", adc_loop, bemf_in_PWM_loop, bemf_loop, bemf_high_loop, CurrentSensing.bZeroCurrentCalibrationStatusFlag);
    printf("system_clock: %d\r\n", system_clock);
    printf("*******\r\n");
    adc_loop = 0;
}
void message_swing_print(void)
{
    printf("***swing_print****\r\n");
    printf(" run: %d,", Motor1.command.u8MotorRunMode); //获取运行状态
    printf("Motor1.IPD.u16DecTimer_1ms:%d , u8RIPDQualifyFailFlag:%d , u16RIPDQualifyFailCounter:%d \r\n", Motor1.IPD.u16DecTimer_1ms, Motor1.IPD.u8RIPDQualifyFailFlag, Motor1.IPD.u16RIPDQualifyFailCounter);
    printf("*******\r\n");
}

void welcome_message_print(void)
{
    printf("*****************welcome_message_print***************\r\n");
    adc_status_message();
}
void current_error_message(void)
{
    printf("***current_error_print****\r\n");
    printf("Motor1.IPD.u16DecTimer_1ms:%d , u8RIPDQualifyFailFlag:%d , u16RIPDQualifyFailCounter:%d \r\n", Motor1.IPD.u16DecTimer_1ms, Motor1.IPD.u8RIPDQualifyFailFlag, Motor1.IPD.u16RIPDQualifyFailCounter);
    tim_status_message();
    printf("*******\r\n");
}
void vsp_print(void)
{
    printf("***vsp_to_stop****\r\n");
    printf("Motor1.IPD.u16DecTimer_1ms:%d , u8RIPDQualifyFailFlag:%d , u16RIPDQualifyFailCounter:%d \r\n", Motor1.IPD.u16DecTimer_1ms, Motor1.IPD.u8RIPDQualifyFailFlag, Motor1.IPD.u16RIPDQualifyFailCounter);
    tim_status_message();
    printf(" Va:%d,Vb:%d,Va_now:%d,Bemf_A_Zero:%d,Bemf_B_zero:%d \r\n***bemf_Rotor_IPD_Mode_Process*****", u16ADC1[BEMFA_ADC_CHANNEL], u16ADC1[BEMFB_ADC_CHANNEL], (uint16_t)BEMFA_ADC_DATA_REGISTER, Motor1.IPD.u16BEMFPhaseAZero12bit, Motor1.IPD.u16BEMFPhaseBZero12bit);
    printf("*******\r\n");
}
void swing_print_message(void)
{

    printf("*********rotor_check********\r\n");
    printf(" run: %d,", Motor1.command.u8MotorRunMode); //获取运行状态
    adc_status_message();
    tim_status_message();
    printf(" Va:%d,Vb:%d,Va_now:%d,Bemf_A_Zero:%d,Bemf_B_zero:%d \r\n", u16ADC1[BEMFA_ADC_CHANNEL], u16ADC1[BEMFB_ADC_CHANNEL], (uint16_t)BEMFA_ADC_DATA_REGISTER, Motor1.IPD.u16BEMFPhaseAZero12bit, Motor1.IPD.u16BEMFPhaseBZero12bit);
    adc_loop_check_message();
    printf("*********rotor_check********\r\n");
}
void IPD_failed_print(void)
{
    printf("*********ipd_failed********\r\n");
    printf(" run: %d,", Motor1.command.u8MotorRunMode); //获取运行状态
    tim_status_message();
    bemf_print_adc();
    adc_loop_check_message();
    printf("*********ipd_failed********\r\n");
}
void standstill_print_message(void)
{
    printf(" run: %d,", Motor1.command.u8MotorRunMode); //获取运行状态
    printf("going to check bemf calibrate mode\r\n");
    adc_loop_check_message();
}
void rotor_position_detect_print_message(void)
{
    printf("*********rotor_position_detect_print_message********\r\n");
    printf(" run: %d,", Motor1.command.u8MotorRunMode); //获取运行状态
    tim_status_message();
    bemf_print_adc();
    adc_loop_check_message();
    printf("*********rotor_position_detect_print_message********\r\n");
}
